{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Openpose1.7.0.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0hLhuliurW6c"
      },
      "source": [
        "# Openpose 1.7.0 Demo"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hp8rTOJ30J-b"
      },
      "source": [
        "# 最初に、ランタイムの種類を「GPU」に変更してください。（ランタイム＞ランタイムのタイプを変更）\n",
        "# GPUが有効になっていることを確認します。\n",
        "\n",
        "# First, change the runtime type to \"GPU\".\n",
        "# Confirm runtime to GPU\n",
        "! nvcc --version\n",
        "! nvidia-smi"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h_ddqkdp0euj"
      },
      "source": [
        "# Openpose バージョン指定タグ\n",
        "# Openpose version tag\n",
        "ver_openpose = \"v1.7.0\"\n",
        "! echo $ver_openpose"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WtlM7y630hKK"
      },
      "source": [
        "# CMakeが古いとOpenpose（CUDA10)が失敗するので、バージョンを確認します。（BugfixはCMake 3.12.3）\n",
        "# If CMake is old, Openpose build fails, so download the latest version\n",
        "# https://developercommunity.visualstudio.com/content/problem/354325/cmake-project-cannot-detect-cuda-10.html\n",
        "! cmake --version"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vjElcN2T0unQ"
      },
      "source": [
        "# 2021.03.03時点の最新CMakeを再ビルド（15分くらい）\n",
        "# Rebuild the latest CMake as of 2021.03.03 (about 15 minutes)\n",
        "! wget -c \"https://github.com/Kitware/CMake/releases/download/v3.19.6/cmake-3.19.6.tar.gz\"\n",
        "! tar xf cmake-3.19.6.tar.gz\n",
        "! cd cmake-3.19.6 && ./configure && make && sudo make install"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nBqcGCVZ1OKs"
      },
      "source": [
        "# ライブラリインストール\n",
        "# Install library\n",
        "\n",
        "# Basic\n",
        "! sudo apt-get --assume-yes update\n",
        "! sudo apt-get --assume-yes install build-essential\n",
        "# OpenCV\n",
        "! sudo apt-get --assume-yes install libopencv-dev\n",
        "# General dependencies\n",
        "! sudo apt-get --assume-yes install libatlas-base-dev libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler\n",
        "! sudo apt-get --assume-yes install --no-install-recommends libboost-all-dev\n",
        "# Remaining dependencies, 14.04\n",
        "! sudo apt-get --assume-yes install libgflags-dev libgoogle-glog-dev liblmdb-dev\n",
        "# Python3 libs\n",
        "! sudo apt-get --assume-yes install python3-setuptools python3-dev build-essential\n",
        "! sudo apt-get --assume-yes install python3-pip\n",
        "! sudo -H pip3 install --upgrade numpy protobuf opencv-python\n",
        "# OpenCL Generic\n",
        "! sudo apt-get --assume-yes install opencl-headers ocl-icd-opencl-dev\n",
        "! sudo apt-get --assume-yes install libviennacl-dev"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AxvRJiaK1QXt"
      },
      "source": [
        "# Openposeのコードをclone\n",
        "# Clone Openpose\n",
        "! git clone  --depth 1 -b \"$ver_openpose\" https://github.com/CMU-Perceptual-Computing-Lab/openpose.git "
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "B7vM17cQ1v3p"
      },
      "source": [
        "# build用ディレクトリを作成\n",
        "# Create build directory\n",
        "! cd openpose && mkdir build && cd build"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bhCJ4zsf2AMa"
      },
      "source": [
        "# https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/installation.md#cmake-command-line-configuration-ubuntu-only\n",
        "# 上記インストール手順のシナリオ１でインストール実行\n",
        "# Scenario 1 - Caffe not installed and OpenCV installed using apt-get\n",
        "! cd openpose/build && cmake .. \n",
        "\n",
        "# COCOモデルのDLオプション付き(「# ! cd」の部分を「! cd」に変更してください。)\n",
        "# If you want to download the COCO model as well, execute the following command. (Please change \"# ! cd\" part to \"! cd\".)\n",
        "# ! cd openpose/build && cmake .. -D DOWNLOAD_BODY_COCO_MODEL=ON\n",
        "\n",
        "# MPIモデルのDLオプション付き（同上）\n",
        "# If you want to download the MPI model as well, execute the following command.\n",
        "# ! cd openpose/build && cmake .. -D DOWNLOAD_BODY_MPI_MODEL=ON"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "L4Zf78976MKN"
      },
      "source": [
        "# Openposeのビルド（15分くらい）\n",
        "# Openpose Building\n",
        "! cd openpose/build && make -j`nproc`\n",
        "# outputフォルダ作成\n",
        "! cd openpose && mkdir output"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FHajFlRk50Ha"
      },
      "source": [
        "# デモ動画を解析\n",
        "# 出力された解析結果は、Colab画面の左メニューのフォルダアイコンから「openpose/output」以下に配置されます。\n",
        "# Run and check the sample\n",
        "# The output analysis result is placed under \"openpose/output\" from the folder icon on the left menu of the Colab screen.\n",
        "! cd openpose && ./build/examples/openpose/openpose.bin --video examples/media/video.avi --display 0  --write_video ./output/openpose.avi"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iu-mWWJmpzCX"
      },
      "source": [
        "# デモ動画の結果をffmpegで再エンコードして表示します。少し時間がかかります。\r\n",
        "# The result of the demo video is re-encoded with ffmpeg and displayed. It will take some time.\r\n",
        "! ffmpeg -i \"./openpose/output/openpose.avi\" -c:v libvpx-vp9 -b:v 1000K -r 30 -threads 8 -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -c:a libopus -b:a 64k -f webm -y out.webm\r\n",
        "\r\n",
        "from IPython.display import HTML\r\n",
        "import base64\r\n",
        "import io\r\n",
        "\r\n",
        "video = io.open('./out.webm', 'r+b').read()\r\n",
        "encoded = base64.b64encode(video)\r\n",
        "data  = '''\r\n",
        "<video id=\"out\" width=\"1280\" height=\"720\" controls><source src=\"data:video/mp4;base64,{0}\" type=\"video/mp4\" /></video>\r\n",
        "'''.format(encoded.decode('ascii'))\r\n",
        "\r\n",
        "HTML(data)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i_MzrC8EHHdv"
      },
      "source": [
        "from google.colab import drive\n",
        "import os\n",
        "\n",
        "# Googleドライブマウント\n",
        "# マウントすると、アクセス許可が求められるので、許可して、キーをコピーして、Colab側の入力欄にCtrl+Vで貼り付けて、Enter。\n",
        "# mount Google Drive\n",
        "# When you mount it, you will be asked for permission, so allow it, copy the key, paste(Ctrl+V) it in the input field on the Colab side, and press Enter.\n",
        "drive.mount('/gdrive')\n",
        "\n",
        "# Google Drive のルートディレクトリ\n",
        "# Google Drive root directory\n",
        "root_path = \"/gdrive/My Drive/\"\n",
        "\n",
        "# Googleドライブ上で解析したい動画パス（この値を好きな値に変更する事で、ご自分のGoogleドライブ上の動画を解析できます）\n",
        "# Video path that you want to analyze on Google Drive (You can analyze the video on your own Google Drive by changing this value to a value you like)\n",
        "file_path = \"autotrace/input.mp4\"\n",
        "\n",
        "# Googleドライブ上の動画を解析（Googleドライブのルートディレクトリに解析結果の「openpose.avi」が出力されます）\n",
        "# Analyze video on Google Drive (The analysis result \"openpose.avi\" is output to the root directory of Google Drive)\n",
        "! cd openpose && ./build/examples/openpose/openpose.bin --video \"$root_path/$file_path\" --display 0  --write_video \"$root_path/openpose.avi\""
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z0FxCZ0BqyA7"
      },
      "source": [
        "# Googleドライブ上の解析結果動画をffmpegで再エンコードして表示します。少し時間がかかります。\r\n",
        "# The analysis result video on Google Drive is re-encoded with ffmpeg and displayed. It will take some time.\r\n",
        "! ffmpeg -i \"$root_path/openpose.avi\" -c:v libvpx-vp9 -b:v 1000K -r 30 -threads 8 -speed 1 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -c:a libopus -b:a 64k -f webm -y out.webm\r\n",
        "\r\n",
        "from IPython.display import HTML\r\n",
        "import base64\r\n",
        "import io\r\n",
        "\r\n",
        "video = io.open('./out.webm', 'r+b').read()\r\n",
        "encoded = base64.b64encode(video)\r\n",
        "data  = '''\r\n",
        "<video id=\"out\" width=\"1280\" height=\"720\" controls><source src=\"data:video/mp4;base64,{0}\" type=\"video/mp4\" /></video>\r\n",
        "'''.format(encoded.decode('ascii'))\r\n",
        "\r\n",
        "HTML(data)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-iJ7aREyJWxv"
      },
      "source": [
        "# 指定した人数分のみ解析したい場合、「number_people_max」の後ろに人数を指定して下さい\n",
        "# If you want to analyze only the specified number of people, please specify the number of people after \"number_people_max\"\n",
        "# 結果はひとつ上のコードを再実行する事でブラウザ上で確認できます。\n",
        "# The result can be confirmed on the browser by re-executing the code one level above.\n",
        "! cd openpose && ./build/examples/openpose/openpose.bin --video \"$root_path/$file_path\" --display 0  --write_video \"$root_path/openpose.avi\" --number_people_max 1"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Nh9f-GTcKJ3u"
      },
      "source": [
        "# 手の解析も一緒に行いたい場合、「--hand」を追加してください\n",
        "# If you also want to analyze the hands together, add \"--hand\"\n",
        "# 結果は２つ上のコードを再実行する事でブラウザ上で確認できます。\n",
        "# The result can be confirmed on the browser by re-executing the code two level above.\n",
        "! cd openpose && ./build/examples/openpose/openpose.bin --video \"$root_path/$file_path\" --display 0  --write_video \"$root_path/openpose.avi\" --hand"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1czsW7anKZbg"
      },
      "source": [
        "# 顔の解析も一緒に行いたい場合、「--face」を追加してください\n",
        "# If you also want to analyze the hands together, add \"--hand\"\n",
        "# 結果は３つ上のコードを再実行する事でブラウザ上で確認できます。\n",
        "# The result can be confirmed on the browser by re-executing the code three level above.\n",
        "! cd openpose && ./build/examples/openpose/openpose.bin --video \"$root_path/$file_path\" --display 0  --write_video \"$root_path/openpose.avi\" --face"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}